<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        // for
        function calculate1(str) {
            var arr = [10];
            for (var i = 0; i != 10; i++) {
                arr[i] = 0;
            }
            for (var i = 0, j = str.length; i != j; i++) {
                var temp = parseInt(str[i]);
                if (temp >= 0 && temp <= 9) {
                    arr[temp]++;
                } else {
                    console.log("数组有错误")
                }
            }
            console.log("最简单的for循环");
            console.log(arr);
        }
        calculate1('12325a664533');

        // foreach  split parseInt
        function calculate2(str) {
            var countnum = new Array(10).fill(0);
            var arr = str.split('');
            arr.forEach(element => {
                var ele = parseInt(element);
                if (ele >= 0 && ele <= 9) {
                    countnum[ele]++
                } else {
                    console.log("数据有错误");
                }
            });
            console.log("用了es6部分语法");
            console.log(countnum);
        }
        calculate2('12325a664533');

        // let => Array.from  Number map
        let calculate3 = str => {
            var countnum = new Array(10).fill(0);
            let arr = Array.from(str);
            arr.map(item => {
                var temp = Number(item);
                if (temp >= 0 && temp <= 9) {
                    countnum[temp]++;
                } else {
                    console.log("数组有错误");
                }
            })
            console.log(countnum);
        }
        calculate3('12325a664533');

        // for in   charCodeAt
        let calculate4 = str => {
            var countnum = new Array(10).fill(0);
            let arr = Array.from(str);
            for (var i in arr) {
                var temp = arr[i].charCodeAt() - 48;
                if (temp >= 0 && temp <= 9) {
                    countnum[temp]++;
                } else {
                    console.log("数组有错误");
                }
            }
            console.log(countnum);
        }
        calculate4('12325a664533');

        // for of  values
        let calculate5 = str => {
            var countnum = new Array(10).fill(0);
            let arr = Array.from(str);
            for (var i of arr.values()) {
                var temp = Number(i);
                if (temp >= 0 && temp <= 9) {
                    countnum[temp]++;
                } else {
                    console.log("数组有错误");
                }
            }
            console.log(countnum);
        }
        calculate5('12325a664533');

        // filter  let
        let calculate6 = str => {
            var countnum = new Array(10).fill(0);
            let arr = Array.from(str);
            for (let i = 0; i != 10; i++) {
                let arr1 = arr.filter(item => {
                    temp = parseInt(item);
                    return (temp == i);
                })
                countnum[i] = arr1.length;
            }
            console.log("这种方式不加额外的判断是不能发现错误的");
            console.log(countnum);
        }
        calculate6('1211325a664533');

        // find
        function finidx(arr, val) {
            var x = arr.findIndex(function (value, index, arr) {
                return value == val;
            })
            return x;
        }

        let calculate7 = str => {
            var countnum = new Array(10).fill(0);
            let arr = Array.from(str);
            // console.log(finidx(arr,1));
            for (let i = 0; i != 10; i++) {
                while (finidx(arr, i) != -1) {
                    countnum[i]++;
                    arr.splice(finidx(arr, i), 1);
                }
            }
            console.log("这是用find来迭代");
            console.log(countnum);
        }
        calculate7('1211325a664533');

        //some
        let calculate8 = str => {
            var arr = Array.from(str);
            var countnum = new Array(10).fill(0);
            for (let i = 0; i != 10; i++) {
                var temparr = str.split(i).join("").length;
                countnum[i] = arr.length - temparr;
            }
            console.log("这是裁剪字符串，然后计算相差的长度");
            console.log(countnum);
        }
        calculate8('1211325a664533');
    </script>
</body>

</html>